/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.dhbw.horb.inf2013.vierFarbenProblem.view.java2D;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import de.dhbw.horb.inf2013.vierFarbenProblem.viewModel.lineToFigure.Point;

/**
 * The Class FreeDrawingField.
 */
public class FreeDrawingField extends JPanel implements MouseListener,
		KeyListener {

	/** The start point. */
	private Point startPoint = null;

	/** The points. */
	private List<Point> points = new LinkedList<Point>();

	/** The figures. */
	private List<Point[]> figures = new LinkedList<Point[]>();

	/**
	 * Instantiates a new free drawing field.
	 */
	public FreeDrawingField() {
		addKeyListener(this);
		addMouseListener(this);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyReleased(KeyEvent evt) {
		System.out.println("leave");
		if (!evt.isControlDown()) {
			points.add(startPoint);

			figures.add(points.toArray(new Point[1]));

			points.clear();
			startPoint = null;
			repaint();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent evt) {
		this.requestFocus();
		if (evt.isControlDown()) {
			Point clickedPoint = new Point(evt.getX(), evt.getY());
			if (startPoint == null) {
				startPoint = clickedPoint;
			}

			points.add(clickedPoint);
		}

		repaint();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyPressed(KeyEvent evt) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyTyped(KeyEvent arg0) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g) {
		g.clearRect(0, 0, 500, 500);
		Iterator<Point> iterator = points.iterator();
		Point lastPoint = null;

		while (iterator.hasNext()) {
			Point currentPoint = iterator.next();
			if (lastPoint == null) {
				lastPoint = currentPoint;
			} else {
				g.drawLine(lastPoint.getX(), lastPoint.getY(),
						currentPoint.getX(), currentPoint.getY());
				lastPoint = currentPoint;
			}
		}

		Iterator<Point[]> iteratorFigures = figures.iterator();

		while (iteratorFigures.hasNext()) {
			Point lastPointFigures = null;
			Point currentPointFigures = null;
			Point[] points = iteratorFigures.next();
			for (int i = 0; i <= points.length - 1; i++) {
				currentPointFigures = points[i];
				if (lastPointFigures == null) {
					lastPointFigures = currentPointFigures;
				} else {
					g.drawLine(lastPointFigures.getX(),
							lastPointFigures.getY(),
							currentPointFigures.getX(),
							currentPointFigures.getY());
					lastPointFigures = currentPointFigures;
				}
			}

		}
	}

}
